import { queryInstDeatilInfo, queryInstOrderInfo } from '../../services/inspect-project'
import { queryOrderById } from '../../services/order'
import { queryVehicleDetail } from '../../services/vehicle'
import { getFileJson, getFileZip } from '../../services/report'
import dayjs from 'dayjs'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    instId:"",
    inspectList:[],
    inspectItem:{},
    token:"",
    basicInfo:{},
    basicList:[
      {
        label:"充电站",
        value:"A充电站"
      },
      {
        label:"检测地址",
        value:"检测地址XXXXXXXXX"
      },
      {
        label:"放电开始时间",
        value:"2023-06-27 14:58:12"
      },
      {
        label:"放电结束时间",
        value:"2023-06-27 14:58:12"
      },
      {
        label:"放电时长",
        value:"62分钟"
      },
      {
        label:"开始SOC",
        value:"90%"
      },
      {
        label:"结束SOC",
        value:"40%"
      },
      {
        label:"放出电能",
        value:"253.00kwh"
      },
      {
        label:"环境温度",
        value:"20℃"
      },
      {
        label:"相对湿度",
        value:"34"
      },
      {
        label:"大气压力",
        value:"100"
      }
    ],
    batteryBasicList:[
      {
        label:"电池生厂商",
        value:"CATL"
      },
      {
        label:"电池类型",
        value:"磷酸铁理电池"
      },
      {
        label:"额定电压",
        value:"352.2V"
      },
      {
        label:"额定容量",
        value:"250.0Ah"
      },
      {
        label:"电池生产日期",
        value:"2021-03-15"
      },
      {
        label:"BMS通信协议版本号",
        value:"V1.1"
      },
      {
        label:'辨识结果',
        value:"BMS能辨识"
      },
      {
        label:"标称能量",
        value:"88.5kWh"
      },
      {
        label:"最高允许充电总电压",
        value:"404.8V"
      },
      {
        label:"单体电池最高允许充电电压",
        value:"3.7V"
      },
      {
        label:"最高允许温度",
        value:"60℃"
      },
      {
        label:"最高允许充电电流",
        value:"****"
      },
    ],
    keyDataList:[
      {
        title:"告警",
        imageUrl:"/assets/report-charging/warning-big.png",
        status:'异常',
        infoList:[
          {
            label:'告警信息',
            value: '1条'
          }
        ]
      },
      {
        title:"极值温度",
        imageUrl:"/assets/report-charging/temp.png",
        status:'正常',
        infoList:[
          {
            label:'温度差',
            value: '3℃'
          },
          {
            label:'单体最高温度',
            value: '36℃'
          },
          {
            label:'单体最低温度',
            value: '33℃'
          },
        ]
      },
      {
        title:"充电速度",
        imageUrl:"/assets/report-charging/charging-speed.png",
        status:'正常',
        infoList:[
          {
            label:'充电速度',
            value: '45.18kW'
          },
        ]
      },
      {
        title:"极值电压",
        imageUrl:"/assets/report-charging/voltage.png",
        status:'正常',
        infoList:[
          {
            label:'电压差',
            value: '0.21V'
          },
          {
            label:'单体最高电压',
            value: '3.86V'
          },
          {
            label:'单体最低电压',
            value: '3.65V'
          },
        ]
      },
      {
        title:"电池一致性",
        imageUrl:"/assets/report-charging/battery-consistency.png",
        status:'正常',
        infoList:[
          {
            label:'电压一致性',
            value: '93.9分'
          },
          {
            label:'温度一致性',
            value: '97分'
          },
        ]
      },
      {
        title:"SOC准确度",
        imageUrl:"/assets/report-charging/soc.png",
        status:'正常',
        infoList:[
          {
            label:'SOC准确度',
            value: '96.29%'
          },
          {
            label:'SOC线性度',
            value: '94.76%'
          },
        ]
      },
      {
        title:"电池健康度",
        imageUrl:"/assets/report-charging/battery.png",
        status:'正常',
        infoList:[
          {
            label:'电池健康度',
            value: '99.24%'
          },
        ]
      },
      {
        title:"BMS测量精度",
        imageUrl:"/assets/report-charging/inspect.png",
        status:'正常',
        infoList:[
          {
            label:'电压精度',
            value: '96.29%'
          },
          {
            label:'电流精度',
            value: '94.76%'
          },
        ]
      },
    ],

    reportInfo:{},
    batteryScoreList:[],
    batteryInfo:{},
    instInfo:{},
    orderInfo: {}


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(!options){
      return
    }

    let instId = options.instId
    let orderId = options.orderId
    this.getInstDeatilInfo(instId)
    this.getOrderDetailInfo(orderId)
    this.setData({
      ['basicInfo.reportId']: instId,
      ['basicInfo.orderId']: orderId
    })

    let jsonUrl = options.jsonUrl
    console.log("jsonUrl>>>", jsonUrl)
    // VehicleDetect/07a7ae3d-c2dcdc65/evalResult/20230815/07a7ae3d-c2dcdc65_20230815124405.zip
    // VehicleDetect/d52bff3-de51c5d6/evalResult/20230728/20230728114454.json
    // 'VehicleDetect/d52bff3-de51c5d6/evalResult/20230720/20230720130743.json'
    if(jsonUrl.indexOf('.json') > -1){
      this.getFileData(jsonUrl)
    } else {
      this.handleGetFileZip(jsonUrl)
    }

  },

  async getInstDeatilInfo(instId){
    let res = await queryInstDeatilInfo(instId)
    console.log("res>>>", res)
    if(res.code === 200){
      let inspectItem = res.data
      // let inspectList = this.dealJson(JSON.parse(inspectItem.itemJson)) 
      this.setData({
        inspectItem,
        // inspectList,
        ['basicInfo.reportDate']: inspectItem.updateTime || "",
        ['basicInfo.detectStartTime']: inspectItem.detectStartTime || "",
        ['basicInfo.detectEndTime']: inspectItem.detectEndTime || "",
      })
      this.setBasicInfo(this.data.reportInfo, inspectItem, this.data.orderInfo)
    }
  },

  async getOrderDetailInfo(orderId){
    let orderRes = await queryOrderById(orderId)
    console.log("orderRes>>>>", orderRes)
    if(orderRes.code === 200){
      let orderInfo = orderRes.data
      let { vehicleId, plateNo } = orderInfo
      this.setData({
        orderInfo,
        ['basicInfo.plateNo']: plateNo || ""
      })
      this.setBasicInfo(this.data.reportInfo, this.data.inspectItem, orderInfo)
      if(vehicleId){
        this.getVehicleDetailInfo(vehicleId)
      }
    }
  },

  async getVehicleDetailInfo(vehicleId){
    let res = await queryVehicleDetail(vehicleId)
    if(res.code === 200){
      let vehicleInfo = res.data
      this.setData({
        vehicleInfo,
        ['basicInfo.vin']: vehicleInfo.vin || ""
      })
    }
  },

  async getFileData(jsonUrl){
    let res = await getFileJson(jsonUrl)
    // 处理电池分项得分
    this.setBatteryInfo(res)
    this.setBasicInfo(res, this.data.inspectItem, this.data.orderInfo)

    this.setData({
      reportInfo: res
    })
  },

  async handleGetFileZip(url){
    let res = await getFileZip({ url })
    if(res.code == 200){
      let data = JSON.parse(res.msg)
      console.log("1213>>>", data)

      this.setBatteryInfo(data)
      this.setBasicInfo(data, this.data.inspectItem, this.data.orderInfo)

      this.setData({
        reportInfo: data,
        ['basicInfo.score']: data.score || ""
      })
    }
  },

  setBatteryInfo(res){
    let batteryScoreList = [
      res.sohValue || 0,
      res.bmsAccuracy || 0,
      res.alarmScore || 0,
      res.uniformityTmp || 0,
      res.uniformityV || 0,
      res.socAccuracy || 0,
    ];
    let batteryInfo = {
      sohValue: res.sohValue || 0, 
      bmsAccuracy: res.bmsAccuracy || 0,
      alarmScore: res.alarmScore || 0,
      uniformityTmp: res.uniformityTmp || 0,
      uniformityV: res.uniformityV || 0,
      socAccuracy: res.socAccuracy || 0,
    }

    let keyDataList = [
      {
        title:"电池健康度",
        imageUrl:"/assets/report-charging/battery.png",
        status: res.sohResult,
        infoList:[
          {
            label:'电池健康度',
            value: `${res.sohValue}%`
          },
        ]
      },
      {
        title:"告警",
        imageUrl:"/assets/report-charging/warning-big.png",
        status: res.alarmResult,
        infoList:[
          {
            label:'告警信息',
            value: `${res.alarmNum}条`
          }
        ]
      },
      {
        title:"SOC准确度",
        imageUrl:"/assets/report-charging/soc.png",
        status: res.socResult,
        infoList:[
          {
            label:'SOC均匀度',
            value: `${res.socLinearity ? Number(res.socLinearity).toFixed(0) : ''}%`
          },
        ]
      },
      {
        title:"BMS测量精度",
        imageUrl:"/assets/report-charging/inspect.png",
        status: res.bmsResult,
        infoList:[
          {
            label: "电压精度",
            value: res.vAccuracy
              ? Number(res.vAccuracy).toFixed(0)
              : "",
            unit: "%",
          },
          {
            label: "电流精度",
            value: res.aAccuracy
              ? Number(res.aAccuracy).toFixed(0)
              : "",
            unit: "%",
          }
        ]
      },
      {
        title:"电池一致性",
        imageUrl:"/assets/report-charging/battery-consistency.png",
        status:'正常',
        infoList:[
          {
            label:'温度一致性',
            value: `${100 - res.tmpDiff}分`
          },
        ]
      },
      {
        title:"放电平均功率",
        imageUrl:"/assets/report-charging/charging-speed.png",
        status: res.chargingSpeedResult,
        infoList:[
          {
            label:'放电平均功率',
            value: `${res.chargingSpeed}kW`
          },
        ]
      },
      {
        title:"极值电压",
        imageUrl:"/assets/report-charging/voltage.png",
        status: res.extremumVResult,
        infoList:[
          {
            label: "电压差",
            value: res.vDiff
              ? Number(res.vDiff).toFixed(2)
              : res.vDiff == 0
              ? 0
              : "",
            unit: "V",
          },
          {
            label: "单体最高电压",
            value: res.cellMaxV,
            unit: "V",
          },
          {
            label: "单体最低电压",
            value: res.cellMinV,
            unit: "V",
          },
        ]
      },
      {
        title:"极值温度",
        imageUrl:"/assets/report-charging/temp.png",
        status: res.extremumTmpResult,
        infoList:[
          {
            label: "温度差",
            value: res.tmpDiff,
            unit: "℃",
          },
          {
            label: "单体最高温度",
            value: res.cellMaxTmp,
            unit: "℃",
          },
          {
            label: "单体最低温度",
            value: res.cellMinTmp,
            unit: "℃",
          },
        ]
      },
    ]

    this.setData({
      batteryScoreList,
      batteryInfo,
      keyDataList
    })
  },

  setBasicInfo(res, instInfo = {}, orderInfo = {}){
    console.log("放电数据res>>>", res)
    let weatherInfo = instInfo.desc ? JSON.parse(instInfo.desc) : {}
    let airTemperature = weatherInfo.airTemperature || ""
    let humidity = weatherInfo.humidity || ""
    let pressure = weatherInfo.pressure || ""

    let basicList = [
      {
        label:"充电站",
        value: res.chargingStation || ""
      },
      {
        label:"检测地址",
        value: orderInfo.stationName || ""
      },
      {
        label:"放电开始时间",
        value: res.startTime ? dayjs(res.startTime).format('YYYY-MM-DD HH:mm:ss') : ""
      },
      {
        label:"放电结束时间",
        value: res.endTime ? dayjs(res.endTime).format('YYYY-MM-DD HH:mm:ss') : ""
      },
      {
        label:"放电时长",
        value: res.duration ? res.duration + '分钟' : ""
      },
      {
        label:"开始SOC",
        value: res.startSoc ? res.startSoc + '%' : ''
      },
      {
        label:"结束SOC",
        value: res.endSoc ? res.endSoc + '%' : ''
      },
      {
        label:"标称总能量",
        value: res.ratedEnergy ? res.ratedEnergy + 'kWh' : ''
      },
      {
        label:"环境温度",
        value: airTemperature ? airTemperature + '℃' : ''   
      },
      {
        label:"相对湿度",
        value: humidity ? humidity + '%' : '' 
      },
      {
        label:"大气压力",
        value: pressure ? pressure + 'kPa' : ''
      },
    ]

    this.setData({
      basicList
    })
  }
})